home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Initialising structure members - help please!
- Date: 20 Feb 1996 17:25:41 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb20102541@qcd.lanl.gov>
- References: <4gb8hn$3m8@news.mistral.co.uk> <31298EF1.5F2C@cmt.lpr.mail.carel.fi>
- <4gckb1$77e@news.mistral.co.uk> <TANMOY.96Feb20094514@qcd.lanl.gov>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: tanmoy@qcd.lanl.gov's message of 20 Feb 1996 16:45:14 GMT
-
- In article <TANMOY.96Feb20094514@qcd.lanl.gov> tanmoy@qcd.lanl.gov
- (Tanmoy Bhattacharya) writes:
- <snip>
- In article <4gckb1$77e@news.mistral.co.uk> mikebarnard@mistral.co.uk
- (Mike Barnard) writes:
- <snip>
- >You can't copy C strings with an "=". You must use the strcpy() function.
-
- and...
-
- >Since you declared 'description' as an array of chars, use strcpy(one.description,
- >"whatever string"); Be sure you won't use longer strings than the space available. A
- >better approach would be to declare 'decription' as a 'char *' and then malloc
-
- Yet better is to declare it const char * then. String literals are
- arrays of type `char []', which in most uses decay to `char*' and can
- be assigned to `const char*'. However, like `const char[]' objects,
- string literals cannot be modified portably. Unlike `const char[]' objects,
- attempts to modify them do not, by themselves, require a diagnostic
- from the compiler.
-
- Assigning them to `const char*' guarantees that unless explicit casts
- (or other tricks involving library functions) are used, any attempt to
- modify them will generate a diagnostic.
-
- >enough space for whatever string you're going to place into it. (Also, your syntax
- >would work in that case.) The last two will work if you add & in front of them.
-
-
- Sorry, omit the garbage I posted above. I did not read carefully
- enough to see that the suggestion was to malloc space. char*x =
- "hello" is unsafe, char *x=malloc(...) followed by if(x) strcpy(...)
- can be absolutely safe. Malloc'ed space is writable of course.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-